//
//  ATRAZMap_DataLoader.m
//  testImage
//
//  Created by lianglibao on 2018/8/31.
//  Copyright © 2018年 lianglibao. All rights reserved.
//

#import "ATRAZMap_DataLoader.h"
#import "ATRAZMapCardModel.h"
#import "ATUtil.h"

@implementation ATRAZMap_DataLoader

+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    static ATRAZMap_DataLoader *shareInstance;
    dispatch_once(&onceToken, ^{
        shareInstance = [[ATRAZMap_DataLoader alloc] init];
    });
    return shareInstance;
}

- (instancetype)init {
    if (self = [super init]) {
        
    }
    return self;
}

- (void)clearCache {
    self.dataSource = nil;
    
}

- (NSArray *)loadRazMapData {
    //  [ATRAZMapModelManager sharedInstance].localRazMapModel.cur_pass = 117;
    //  [ATRAZMapModelManager sharedInstance].localRazMapModel.cur_passing = 32;
    
    @autoreleasepool {
        NSString *deviceString;
        if ([ATUtil isiPhoneXR]) {
            deviceString = @"iPhoneXR";
        } else if ([ATUtil isiPhoneX]) {
            deviceString = @"iPhoneX";
        } else if ([ATUtil isiPhoneSize_5_5]) {
            deviceString = @"iPhone5_5";
        } else if ([ATUtil isiPhoneSize_4_7]) {
            deviceString = @"iPhone4_7";
        } else if ([ATUtil isiPhoneSize_4_0]) {
            deviceString = @"iPhone4_0";
        } else if ([ATUtil isiPhoneSize_3_5]) {
            deviceString = @"iPhone3_5";
        } else {
            deviceString = @"iPad";
        }
        
        NSString *basePath = $(@"%@/maps/%@/", [ATUtil getCachePath], deviceString);
        
        UIImage *cardDontImage;
        UIImage *cardDoneImage;
        UIImage *guidePostImage;
        NSMutableArray *tempModels = [NSMutableArray array];
        UIImage   *bgCoverImage;
        UIImage *cardSuperDontImage;
        UIImage *cardSuperDoneImage;
        UIImage *stationImage;
        UIImage *waterDoneImage;
        UIImage *waterDontImage;
        __block NSInteger cardStarIndex = 0;
        NSString *cardPointpath;
        NSData *cardPointData;
        NSArray *rectArray;
        CGFloat scale = [UIScreen mainScreen].scale;
        CGFloat subjectWidth = 0;
        NSString *sourcePath;
        UIImage *image;
        NSError *error;
        if (!self.dataSource.count) {
            
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_one_bg.png"
                           );
            image = [UIImage imageWithContentsOfFile:sourcePath];
            image = [UIImage imageWithCGImage:image.CGImage scale:scale
                                  orientation:UIImageOrientationUp];
            subjectWidth = image.size.width;
            
            // 加载card Dont image
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_normal_card_dont.png"
                           );
            cardDontImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card Done image
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_normal_card_done.png"
                           );
            cardDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card cardSuperDoneImage
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_super_card_done.png"
                           );
            cardSuperDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card cardSuperDontImage
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_super_card_dont.png"
                           );
            cardSuperDontImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载站台 stationImage
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_whirligig_station.png"
                           );
            stationImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载水中关卡图片(可点击)
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_water_card_done.png"
                           );
            waterDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载水中关卡图片(不可可点击)
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_water_card_dont.png"
                           );
            waterDontImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card point
            cardPointpath = $(@"%@%@%@",
                              basePath,
                              deviceString,
                              @"_subject_one_card_point.json"
                              );
            cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
            rectArray = [NSJSONSerialization JSONObjectWithData:cardPointData
                                                        options:0
                                                          error:&error];
            if (error) {
                NSLog(@"%@", error);
            }
            
            // 设置cardStarIndex
            cardStarIndex = 0;
            
            // 生成model
            ATRAZMapCardModel *modelOne = [ATRAZMapCardModel new];
            modelOne.subjectWidth    = subjectWidth;
            modelOne.cardDontImage   = cardDontImage;
            modelOne.cardDoneImage   = cardDoneImage;
            modelOne.cardCenterFrame = rectArray;
            modelOne.cardStartIndex  = cardStarIndex;
            modelOne.guidePostImage  = guidePostImage;
            modelOne.cardSuperDontImage = cardSuperDontImage;
            modelOne.cardSuperDoneImage = cardSuperDoneImage;
            modelOne.cardStationImage   = stationImage;
            modelOne.cardWaterDoneImage = waterDoneImage;
            modelOne.cardWaterDontImage = waterDontImage;
            modelOne.cardBgImageName    = $(@"%@%@%@",
                                            basePath,
                                            deviceString,@"_subject_one_bg.png"
                                            );
            
            [tempModels addObject:modelOne];
            self.dataSource = tempModels.copy;
        }
        
        // 第2个地图数据====================================================
        if ((self.dataSource.count == 1)) {
            // 更新所有subject Model 数组
            if (!tempModels.count) {
                [tempModels addObjectsFromArray:self.dataSource];
            }
            
            // 更新第二个subject Model 的开始起点值
            [tempModels enumerateObjectsUsingBlock:^(ATRAZMapCardModel *obj,
                                                     NSUInteger idx,
                                                     BOOL * _Nonnull stop) {
                cardStarIndex += obj.cardCenterFrame.count;
            }];
            
            sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_two_bg.png");
            image = [UIImage imageWithContentsOfFile:sourcePath];
            image = [UIImage imageWithCGImage:image.CGImage
                                        scale:scale
                                  orientation:UIImageOrientationUp];
            subjectWidth = image.size.width;
            
            // 加载cell连接处cover bg
            bgCoverImage = [UIImage imageNamed:@"subject_two_cover_bg"];
            
            // 加载card Dont image
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_two_dont.png"
                           );
            cardDontImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card Done image
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_two_done.png"
                           );
            cardDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card cardSuperDontImage
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_two_super_dont.png"
                           );
            cardSuperDontImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card cardSuperDoneImage
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_two_super_done.png"
                           );
            cardSuperDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card point
            cardPointpath = $(@"%@%@%@", basePath,
                              deviceString,
                              @"_subject_two_card_point.json"
                              );
            cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
            rectArray = [NSJSONSerialization JSONObjectWithData:cardPointData
                                                        options:0
                                                          error:&error];
            if (error) {
                NSLog(@"%@", error);
            }
            
            // 生成model
            ATRAZMapCardModel *modelTwo = [ATRAZMapCardModel new];
            modelTwo.subjectWidth    = subjectWidth;
            modelTwo.cardDontImage   = cardDontImage;
            modelTwo.cardDoneImage   = cardDoneImage;
            modelTwo.cardCenterFrame = rectArray;
            modelTwo.cardStartIndex  = cardStarIndex;
            modelTwo.guidePostImage  = guidePostImage;
            modelTwo.bgLayerImage    = bgCoverImage;
            modelTwo.cardSuperDontImage = cardSuperDontImage;
            modelTwo.cardSuperDoneImage = cardSuperDoneImage;
            modelTwo.cardBgImageName    = $(@"%@%@%@", basePath, deviceString, @"_subject_two_bg.png");
            
            [tempModels addObject:modelTwo];
            self.dataSource = tempModels.copy;
        }
        
        // 第3个地图数据====================================================
        if ((self.dataSource.count == 2)) {
            if (!tempModels.count) {
                [tempModels addObjectsFromArray:self.dataSource];
            }
            // 更新第二个subject Model 的开始起点值
            cardStarIndex = 0;
            [tempModels enumerateObjectsUsingBlock:^(ATRAZMapCardModel *obj,
                                                     NSUInteger idx,
                                                     BOOL * _Nonnull stop) {
                cardStarIndex += obj.cardCenterFrame.count;
            }];
            
            sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_three_bg.png");
            image = [UIImage imageWithContentsOfFile:sourcePath];
            image = [UIImage imageWithCGImage:image.CGImage scale:scale orientation:UIImageOrientationUp];
            subjectWidth = image.size.width;
            
            // 加载cell连接处cover bg
            bgCoverImage = [UIImage imageNamed:@"subject_three_cover_bg"];
            
            // 加载card Dont image
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_three_dont.png"
                           );
            cardDontImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card Done image
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_three_done.png"
                           );
            cardDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card cardSuperDontImage
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_three_super_dont.png"
                           );
            cardSuperDontImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card cardSuperDoneImage
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_three_super_done.png"
                           );
            cardSuperDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card point
            cardPointpath = $(@"%@%@%@",
                              basePath,
                              deviceString,
                              @"_subject_three_card_point.json"
                              );
            cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
            rectArray = [NSJSONSerialization JSONObjectWithData:cardPointData
                                                        options:0
                                                          error:&error];
            if (error) {
                NSLog(@"%@", error);
            }
            
            // 生成model
            ATRAZMapCardModel *modelThree = [ATRAZMapCardModel new];
            modelThree.subjectWidth    = subjectWidth;
            modelThree.cardDontImage   = cardDontImage;
            modelThree.cardDoneImage   = cardDoneImage;
            modelThree.cardCenterFrame = rectArray;
            modelThree.cardStartIndex  = cardStarIndex;
            modelThree.guidePostImage  = guidePostImage;
            modelThree.cardSuperDontImage = cardSuperDontImage;
            modelThree.cardSuperDoneImage = cardSuperDoneImage;
            modelThree.bgLayerImage       = bgCoverImage;
            modelThree.cardBgImageName    = $(@"%@%@%@",
                                              basePath,
                                              deviceString,
                                              @"_subject_three_bg.png"
                                              );
            
            [tempModels addObject:modelThree];
            self.dataSource = tempModels.copy;
        }
        
        // 第4个地图数据====================================================
        // 加载第4个地图数据
        if ((self.dataSource.count == 3)) {
            if (!tempModels.count) {
                [tempModels addObjectsFromArray:self.dataSource];
            }
            // 更新第二个subject Model 的开始起点值
            cardStarIndex = 0;
            [tempModels enumerateObjectsUsingBlock:^(ATRAZMapCardModel *obj,
                                                     NSUInteger idx,
                                                     BOOL * _Nonnull stop) {
                cardStarIndex += obj.cardCenterFrame.count;
            }];
            
            sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_four_bg.png");
            image = [UIImage imageWithContentsOfFile:sourcePath];
            image = [UIImage imageWithCGImage:image.CGImage
                                        scale:scale
                                  orientation:UIImageOrientationUp];
            subjectWidth = image.size.width;
            
            // 加载cell连接处cover bg
            bgCoverImage = [UIImage imageNamed:@"subject_four_cover_bg"];
            
            // 加载card Dont image
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_four_dont.png"
                           );
            cardDontImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card Done image
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_four_done.png"
                           );
            cardDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card cardSuperDontImage
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_four_super_dont.png"
                           );
            cardSuperDontImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card cardSuperDoneImage
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_four_super_done.png"
                           );
            cardSuperDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
            
            // 加载card point
            cardPointpath = $(@"%@%@%@",
                              basePath,
                              deviceString,
                              @"_subject_four_card_point.json"
                              );
            cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
            rectArray = [NSJSONSerialization JSONObjectWithData:cardPointData
                                                        options:0
                                                          error:&error];
            if (error) {
                NSLog(@"%@", error);
            }
            
            // 生成model
            ATRAZMapCardModel *modelFour = [ATRAZMapCardModel new];
            modelFour.subjectWidth    = subjectWidth;
            modelFour.cardDontImage   = cardDontImage;
            modelFour.cardDoneImage   = cardDoneImage;
            modelFour.cardCenterFrame = rectArray;
            modelFour.cardStartIndex  = cardStarIndex;
            modelFour.guidePostImage  = guidePostImage;
            modelFour.cardSuperDontImage = cardSuperDontImage;
            modelFour.cardSuperDoneImage = cardSuperDoneImage;
            modelFour.bgLayerImage       = bgCoverImage;
            modelFour.cardBgImageName   = $(@"%@%@%@",
                                            basePath,
                                            deviceString,
                                            @"_subject_four_bg.png"
                                            );
            
            [tempModels addObject:modelFour];
            self.dataSource = tempModels.copy;
        }
        
        // 第5个地图数据====================================================
        if ((self.dataSource.count == 4)) {
            if (!tempModels.count) {
                [tempModels addObjectsFromArray:self.dataSource];
            }
            // 更新第二个subject Model 的开始起点值
            cardStarIndex = 0;
            [tempModels enumerateObjectsUsingBlock:^(ATRAZMapCardModel *obj,
                                                     NSUInteger idx,
                                                     BOOL * _Nonnull stop) {
                cardStarIndex += obj.cardCenterFrame.count;
            }];
            
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_five_bg.png"
                           );
            image = [UIImage imageWithContentsOfFile:sourcePath];
            image = [UIImage imageWithCGImage:image.CGImage
                                        scale:scale
                                  orientation:UIImageOrientationUp];
            subjectWidth = image.size.width;
            
            // 加载card point
            cardPointpath = $(@"%@%@%@",
                              basePath,
                              deviceString,
                              @"_subject_five_card_point.json"
                              );
            cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
            rectArray = [NSJSONSerialization JSONObjectWithData:cardPointData
                                                        options:0
                                                          error:&error];
            if (error) {
                NSLog(@"%@", error);
            }
            
            // 生成model 与 第2个model相同
            ATRAZMapCardModel *model2 = self.dataSource[1];
            ATRAZMapCardModel *modelFive = [ATRAZMapCardModel new];
            modelFive.cardStartIndex  = cardStarIndex;
            modelFive.subjectWidth    = subjectWidth;
            modelFive.cardDontImage   = model2.cardDontImage;
            modelFive.cardDoneImage   = model2.cardDoneImage;
            modelFive.cardCenterFrame = rectArray;
            modelFive.cardStartIndex  = cardStarIndex;
            modelFive.cardSuperDontImage = model2.cardSuperDontImage;
            modelFive.cardSuperDoneImage = model2.cardSuperDoneImage;
            modelFive.bgLayerImage       = model2.bgLayerImage;
            modelFive.cardBgImageName    = $(@"%@%@%@",
                                             basePath,
                                             deviceString,
                                             @"_subject_five_bg.png"
                                             );
            
            [tempModels addObject:modelFive];
            self.dataSource = tempModels.copy;
        }
        
        // 第6个地图数据====================================================
        if ((self.dataSource.count == 5)) {
            if (!tempModels.count) {
                [tempModels addObjectsFromArray:self.dataSource];
            }
            // 更新第二个subject Model 的开始起点值
            cardStarIndex = 0;
            [tempModels enumerateObjectsUsingBlock:^(ATRAZMapCardModel *obj,
                                                     NSUInteger idx,
                                                     BOOL * _Nonnull stop) {
                cardStarIndex += obj.cardCenterFrame.count;
            }];
            
            sourcePath = $(@"%@%@%@",
                           basePath,
                           deviceString,
                           @"_subject_six_bg.png"
                           );
            image = [UIImage imageWithContentsOfFile:sourcePath];
            image = [UIImage imageWithCGImage:image.CGImage
                                        scale:scale
                                  orientation:UIImageOrientationUp];
            subjectWidth = image.size.width;
            
            // 加载card point
            cardPointpath = $(@"%@%@%@",
                              basePath,
                              deviceString,
                              @"_subject_six_card_point.json"
                              );
            cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
            rectArray = [NSJSONSerialization JSONObjectWithData:cardPointData
                                                        options:0
                                                          error:&error];
            if (error) {
                NSLog(@"%@", error);
            }
            
            // 生成model 与 第2个model相同
            ATRAZMapCardModel *model3 = self.dataSource[2];
            ATRAZMapCardModel *modelSix = [ATRAZMapCardModel new];
            modelSix.cardStartIndex  = cardStarIndex;
            modelSix.subjectWidth    = subjectWidth;
            modelSix.cardDontImage   = model3.cardDontImage;
            modelSix.cardDoneImage   = model3.cardDoneImage;
            modelSix.cardCenterFrame = rectArray;
            modelSix.cardStartIndex  = cardStarIndex;
            modelSix.cardSuperDontImage = model3.cardSuperDontImage;
            modelSix.cardSuperDoneImage = model3.cardSuperDoneImage;
            modelSix.bgLayerImage       = model3.bgLayerImage;
            modelSix.cardBgImageName    = $(@"%@%@%@",
                                            basePath,
                                            deviceString,
                                            @"_subject_six_bg.png"
                                            );
            
            [tempModels addObject:modelSix];
            self.dataSource = tempModels.copy;
            
        }
        
    }
    
    return self.dataSource;
}

- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
}

- (void)updateDataSource {
    
}

@end
